package com.juner.api.enums;

import cn.hutool.extra.spring.SpringUtil;
import com.juner.api.strategies.A;
import com.juner.api.strategies.B;
import com.juner.api.strategies.C;
import com.juner.api.strategies.StratetyService;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

/**
 * @author DengJunPeng
 * @effect: 基于注解方式的策略模式
 * 只需修改策略枚举类即可
 * @e-mail 3335056441@qq.com
 * @date 2024/10/13 20:26
 */
@AllArgsConstructor
@Getter
public enum StrategyEnum {
    Strategy_A("A", A.class, "a描述"),
    Strategy_B("B", B.class, "B描述"),
    Strategy_C("C", C.class, "C描述");

    private final String value; // 策略值
    private final Class<?> strategyService;
    private final String description;

    private static final Map<StrategyEnum, Class<?>> STRATEGY_MAP = new HashMap<>();

    static {
        for (StrategyEnum strategyEnum : StrategyEnum.values()) {
            STRATEGY_MAP.put(strategyEnum, strategyEnum.getStrategyService());
        }
    }

    public static StrategyEnum getEnumWithValue(String type) {
        for (StrategyEnum strategyEnum : StrategyEnum.values()) {
            if (strategyEnum.getValue().equals(type)) {
                return strategyEnum;
            }
        }
        return null;
    }

    public StratetyService getEnumStrategyService() {
        return (StratetyService) SpringUtil.getBean(STRATEGY_MAP.get(this));
    }

}
